package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.bean.Category;
import com.itheima.dto.SetmealDto;
import com.itheima.bean.PageParam;
import com.itheima.bean.Setmeal;
import com.itheima.bean.SetmealDish;
import com.itheima.dao.SetmealDao;
import com.itheima.exception.CustomException;
import com.itheima.service.CategoryService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Transactional
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealDao, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    //根据ID查找套餐数据
    @Override
    public List<Setmeal> findByCid(long cid) {

        LambdaQueryWrapper<Setmeal> lwq = new LambdaQueryWrapper<>();

        lwq.eq(Setmeal::getCategoryId,cid);
        return list(lwq);
    }


    //添加套餐
    @Override
    public boolean add(SetmealDto setmealDto) {
        //1.往套餐表添加数据
        boolean result1 = save(setmealDto);

        //2.往菜品表添加数据

        //2.1获取菜品数据
        List<SetmealDish> dishList = setmealDto.getSetmealDishes();

        //2.2菜品数据添加要为他们设置id
        List<SetmealDish> list = dishList.stream().map(setmealDish -> {
            //设置套餐的ID
            setmealDish.setSetmealId(setmealDto.getId());

            return setmealDish;
        }).collect(Collectors.toList());

        boolean result2 = setmealDishService.saveBatch(list);

        return result1 && result2;
    }

    //套餐分页查询
    @Override
    public IPage<SetmealDto> page(PageParam pageParam) {
        //1.先查询套餐表,进行分页查询
        Page<Setmeal> page = new Page<>(pageParam.getPage(),pageParam.getPageSize());

        //1.2构建条件对象
        LambdaQueryWrapper<Setmeal> lwq = new LambdaQueryWrapper<>();

        //1.3设置条件
        lwq.like(pageParam.getName() != null, Setmeal::getName,pageParam.getName());

        //1.4执行分页查询
        page(page,lwq);

        //2.不能直接返回,需要构建IPage<SemtealDto>对象
        Page<SetmealDto> dtoPage = new Page<>();

        //2.1组装total属性
            dtoPage.setTotal(page.getTotal());

        //2.2组装records属性

        //2.2.1把List<Setmeal> 映射成 List<SetmealDto>
        List<SetmealDto> list = page.getRecords().stream().map(setmeal -> {

            //a. 将一个Setmeal对象映射成一个SetmealSto对象
            SetmealDto dto = new SetmealDto();

            //b. 把Setmeal数据拷贝到dto来
            BeanUtils.copyProperties(setmeal, dto);

            //c. 查询分类数据,把分类名称储存到dto
            Category category = categoryService.getById(setmeal.getCategoryId());
            dto.setCategoryName(category.getName());
            return dto;

        }).collect(Collectors.toList());

        dtoPage.setRecords(list);
        return dtoPage;
    }

    //删除套餐
    @Override
    public boolean delete(List<Long> ids) {
        //1.先检查这一批ID里面有没有处于售卖的状态

        //1.1构建条件对象
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();

        //1.2设置条件
        lqw.eq(Setmeal::getStatus,1);
        lqw.in(Setmeal::getId,ids);

        //1.3 执行查询
        List<Setmeal> list = list(lqw);

        //如果有套餐属于售卖状态
        if(list != null && list.size()>0){
            //禁止删除
            throw new CustomException("有套餐处于售卖状态,禁止删除!");
        }

        //2.允许删除
        return removeBatchByIds(ids);

    }


}
